package cj.web.sc.controllers;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.ReturnUUID;
import cj.web.cc.controllers.BaseController;
import cj.web.cc.dao.ApplyDAO;
import cj.web.cc.dao.MemberDAO;
import cj.web.constants.MemuEnum;
import cj.web.constants.SCOrderStatusEnum;
import cj.web.game.dao.GameDAO;
import cj.web.sc.dao.GoodsDAO;
import cj.web.sc.dao.OrderDAO;
import cj.web.sc.dao.SCCrowdFundOrderDAO;

@Path("orderPc")
public class OrderPcController extends BaseController{
	@Autowired
	private GoodsDAO goodsDao;
	@Autowired
	private OrderDAO orderDao;
	@Autowired
	private MemberDAO memberDAO;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private SCCrowdFundOrderDAO scCrowdFundOrderDao;
	@Autowired
	private ApplyDAO applyDao;
	
	@Get("orderConfirm") //确认订单
	public String orderConfirm(Model model, @Param("goodsItemIds")String goodsItemIds, 
			@Param("counts")String counts, HttpServletRequest request,HttpServletResponse response) throws Exception{
		model.add("currentTopMemu", MemuEnum.memuccsd);
		model.add("logisticsPrice", LOGISTICS_FEE);
		if( isLogin(request) ){
			int memberId = Integer.parseInt(getCookieValueByName(request, "memberId"));
			model.add("buyerId", memberId);
			
			//地址初始化
			List<Map> address = memberDAO.getFirstAddress(memberId);
			if(address != null && address.size() > 0){
				model.add("Address", address.get(0));
			}
			
			//商品初始化
			if(goodsItemIds != null && goodsItemIds.length() > 0 && 
					counts != null && counts.length() > 0){
				String[] itemIds = goodsItemIds.split(",");
				String[] itemCnts = counts.split(",");
				List<Map> objs = new ArrayList<Map>();
				if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
						itemCnts.length > 0 && itemIds.length == itemCnts.length){
					BigDecimal totalPrice = new BigDecimal(0);
					for(int i = 0; i < itemIds.length; i++){
						int itemId = Integer.parseInt(itemIds[i]);
						int count = Integer.parseInt(itemCnts[i]);
						List<Map> goodsItem = goodsDao.getGoodsItemById(itemId);
						if(goodsItem != null && goodsItem.size() > 0){
							Map obj = goodsItem.get(0);
							int inventory = Integer.parseInt(obj.get("inventory").toString());  //验证库存
							if(inventory < count){
								String goodsName = obj.get("name").toString();
								model.add("warning", goodsName + "库存不足");
								return "/views/mallPC/cartNothing.vm";
							}
							obj.put("propValueNames", obj.get("propValueNames").toString().replaceAll(",", ""));
							obj.put("count", count);
							BigDecimal priceBD = new BigDecimal(obj.get("discntPrice").toString());
							BigDecimal price = priceBD.multiply(BigDecimal.valueOf(count)); //单价×数量
							totalPrice = totalPrice.add(price);
							obj.put("inlinePrice", priceFormat(price.doubleValue()));
							objs.add(obj);
						}
						else{
							model.add("warning", "该商品已下架");
							return "/views/mallPC/cartNothing.vm";
						}
					}
					totalPrice = totalPrice.add(new BigDecimal(LOGISTICS_FEE));
					model.add("totalPrice", priceFormat(totalPrice.doubleValue()));
				}
				else{
					model.add("warning", "该商品已下架");
					return "/views/mallPC/cartNothing.vm";
				}
				model.add("Objs",objs);
			
				if(objs.size() > 0){
					int sellerId = Integer.parseInt(objs.get(0).get("sellerId").toString());
					String sellerName = goodsDao.getSellerName(sellerId);
					model.add("sellerId",sellerId);
					model.add("sellerName",sellerName);
				}
			}
			else{
				model.add("warning", "该商品已下架");
				return "/views/mallPC/cartNothing.vm";
			}
			
			model.add("goodsItemIds",goodsItemIds);
			model.add("counts",counts);
			String orderNo = ReturnUUID.getOrderNo(ReturnUUID.getRandomNum());
			model.add("orderNo",orderNo);
			
			return "/views/mallPC/orderConfirm.vm";
		}
		else{
			String retPg = "/orderPc/orderConfirm?goodsItemIds=" + goodsItemIds + "&counts=" + counts;
			setCookie(response, "returnPage", retPg);//订单
			return "r:../user/loginPage";
		}
	}
	
	@Get("orderZCConfirm") //确认订单
	public String orderZCConfirm(Model model, @Param("goodsItemIds")String goodsItemIds, 
			@Param("counts")String counts, HttpServletRequest request,HttpServletResponse response) throws Exception{
		model.add("currentTopMemu", MemuEnum.memuccsd);
		model.add("logisticsPrice", 0);
		if( isLogin(request) ){
			int memberId = Integer.parseInt(getCookieValueByName(request, "memberId"));
			model.add("buyerId", memberId);
			
			//地址初始化
			List<Map> address = memberDAO.getFirstAddress(memberId);
			if(address != null && address.size() > 0){
				model.add("Address", address.get(0));
			}
			
			//商品初始化
			if(goodsItemIds != null && goodsItemIds.length() > 0 && 
					counts != null && counts.length() > 0){
				String[] itemIds = goodsItemIds.split(",");
				String[] itemCnts = counts.split(",");
				List<Map> objs = new ArrayList<Map>();
				if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
						itemCnts.length > 0 && itemIds.length == itemCnts.length){
					BigDecimal totalPrice = new BigDecimal(0);
					for(int i = 0; i < itemIds.length; i++){
						int itemId = Integer.parseInt(itemIds[i]);
						int count = Integer.parseInt(itemCnts[i]);
						List<Map> goodsItem = goodsDao.getGoodsItemById(itemId);
						if(goodsItem != null && goodsItem.size() > 0){
							Map obj = goodsItem.get(0);
							int inventory = Integer.parseInt(obj.get("inventory").toString());  //验证库存
							if(inventory < count){
								String goodsName = obj.get("name").toString();
								model.add("warning", goodsName + "库存不足");
								return "/views/mallPC/cartNothing.vm";
							}
							obj.put("propValueNames", obj.get("propValueNames").toString().replaceAll(",", ""));
							obj.put("count", count);
							BigDecimal priceBD = new BigDecimal(obj.get("discntPrice").toString());
							BigDecimal price = priceBD.multiply(BigDecimal.valueOf(count)); //单价×数量
							totalPrice = totalPrice.add(price);
							obj.put("inlinePrice", priceFormat(price.doubleValue()));
							objs.add(obj);
						}
						else{
							model.add("warning", "该商品已下架");
							return "/views/mallPC/cartNothing.vm";
						}
					}
					totalPrice = totalPrice.add(new BigDecimal(0));
					model.add("totalPrice", priceFormat(totalPrice.doubleValue()));
				}
				else{
					model.add("warning", "该商品已下架");
					return "/views/mallPC/cartNothing.vm";
				}
				model.add("Objs",objs);
			
				if(objs.size() > 0){
					int sellerId = Integer.parseInt(objs.get(0).get("sellerId").toString());
					String sellerName = goodsDao.getSellerName(sellerId);
					model.add("sellerId",sellerId);
					model.add("sellerName",sellerName);
				}
			}
			else{
				model.add("warning", "该商品已下架");
				return "/views/mallPC/cartNothing.vm";
			}
			
			model.add("goodsItemIds",goodsItemIds);
			model.add("counts",counts);
			
			//活动列表
			List<Map> gamels = gameDao.queryAll();
			model.add("gamels",gamels);
			model.add("urlGameId",getCookieValueByName(request, "urlGameId"));
			model.add("urlCode",getCookieValueByName(request, "urlCode"));
			
			String orderNo = ReturnUUID.getOrderNo(ReturnUUID.getRandomNum());
			model.add("orderNo",orderNo);
			
			return "/views/mallPC/orderZCConfirm.vm";
		}
		else{
			String retPg = "/orderPc/orderZCConfirm?goodsItemIds=" + goodsItemIds + "&counts=" + counts;
			setCookie(response, "returnPage", retPg);//订单
			return "r:../user/loginPage";
		}
	}
	
	/**
	 * 返回获奖信息
	 * @param gameId
	 * @param code
	 * @return
	 */
	@Post("getLevelByCAI")
	public String getLevelByCAI(@Param("gameId")int gameId,@Param("code")String code){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> reps = applyDao.queryByGameIdAndCode(gameId, code);
		if(reps!=null && reps.size()>0){
			jo.put("userGameInfor", reps.get(0));
		}else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo.toString();
	}
	
	@Post("getAddrById")
	public String getAddrById(@Param("addressId")String addressId){
		JSONObject jo = new JSONObject();
		List<Map> address = memberDAO.getAddressById(addressId);
		jo.put("address", address);
		return "@" + jo.toString();
	}
	
	@Post("addrLst")
	public String addressList(HttpServletRequest request){
		JSONObject jo = new JSONObject();
		int memberId = Integer.parseInt(getCookieValueByName(request, "memberId"));
		List<Map> addressList = memberDAO.getAddrByBuyerId(memberId);
		jo.put("addressList", addressList);
		return "@" + jo.toString();
	}
	
	@Post("newAddr")
	public String newAddr(@Param("receiverName")String receiverName, @Param("receiverPhone")String receiverPhone, 
			@Param("province")String province, @Param("city")String city, @Param("area")String area, 
			@Param("address")String address,@Param("isDefault")int isDefault, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		int memberId = Integer.parseInt(getCookieValueByName(request, "memberId"));
		if(isDefault == 1){//把默认置为非默认
			memberDAO.notDefaultByBuyerId(memberId);
		}
		int addressId = memberDAO.createAddress(memberId, receiverName, receiverPhone, province, city, area, address, isDefault, receiverPhone);
		jo.put("addressId", addressId);
		return "@" + jo.toString();
	}
	
	@Post("modifyAddr")
	public String modifyAddr(@Param("id")String id, @Param("receiverName")String receiverName, @Param("receiverPhone")String receiverPhone, 
			@Param("province")String province, @Param("city")String city, @Param("area")String area, 
			@Param("address")String address,@Param("isDefault")int isDefault, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		int memberId = Integer.parseInt(getCookieValueByName(request, "memberId"));
		if(isDefault == 1){//把默认置为非默认
			memberDAO.notDefaultByBuyerId(memberId);
		}
		memberDAO.updateAddress(id, receiverName, receiverPhone, province, city, area, address, isDefault);
		return "@" + jo.toString();
	}
	
	@Get("cart") //购物车
	public String cart(Model model, HttpServletRequest request,HttpServletResponse response){
		model.add("currentTopMemu", MemuEnum.memuccsd);
		model.add("currentMemu", MemuEnum.wdgwc);
		model.add("logisticsPrice", LOGISTICS_FEE);
		if( isLogin(request) ){
			String memberId = getCookieValueByName(request, "memberId");
			//购物车商家信息
			List<Map> cartSellerObjs = orderDao.cartSellerByBuyerId(Integer.parseInt(memberId));
			//购物车商品列表信息
			List<Map> cartObjs = orderDao.cartByBuyerId(Integer.parseInt(memberId));
			if(cartObjs != null && cartObjs.size() > 0){
				
				for(Map cartSeller:cartSellerObjs){
					//商家ID
					int sellerId = Integer.parseInt(cartSeller.get("sellerId").toString());//我的购物车中商家信息
					List<Map> goodsItem = new ArrayList<Map>();
					for(Map rep:cartObjs){
						int goodsSellerId = Integer.parseInt(rep.get("sellerId").toString());//商品列表中的商家ID
						if(sellerId==goodsSellerId){
							//购物车 价格
							BigDecimal discntPrice = new BigDecimal(rep.get("discntPrice").toString());
							BigDecimal count = new BigDecimal(rep.get("count").toString());
							BigDecimal bignum3 = discntPrice.multiply(count);  //discntPrice*count
							rep.put("discntPriceSum", bignum3);//实付价格
							goodsItem.add(rep);
						}
					}
					cartSeller.put("goodsItems", goodsItem);
				}
				
				model.add("cartSellerObjs", cartSellerObjs);
			}
			else{
				model.add("warning", "购物车空空如也~");
				return "/views/mallPC/cartNothing.vm";
			}
			
		}
		else{
			setCookie(response, "returnPage", "/orderPc/cart");//订单
			return "r:../user/loginPage";
		}
		
		return "/views/mallPC/shoppingCart.vm";
	}
	
	/**
	 * 减
	 * @param model
	 * @param cartId
	 * @return
	 */
	@Post("cartNumUpdate")
	public String numberPlus(Model model,@Param("cartId")int cartId,@Param("count")int count){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		orderDao.updateCartById(cartId, count);
		
		return "@" + jo.toString();
	}
	
	@Get("emptyCart")
	public String emptyCart(Model model){
		model.add("warning", "购物车空空如也~");
		model.add("currentMemu", MemuEnum.wdgwc);
		model.add("currentTopMemu", MemuEnum.memuccsd);
		return "/views/mallPC/cartNothing.vm";
	}
	
	@Post("delCart")
	public String delCart(@Param("ids")String ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		JSONArray joIds = JSONArray.fromObject(ids);
		List<Integer> arrIds = new ArrayList<Integer>();
		for(int i = 0; i < joIds.size(); i++){
			arrIds.add(joIds.getInt(i));
		}
		orderDao.delCartByIds(arrIds);
		
		return "@" + jo.toString();
	}
	
	@Post("settleAccounts")
	public String settleAccounts(@Param("sellerId")int sellerId, @Param("addressId")String addressId, @Param("memo")String memo, @Param("goodsItemIds")String goodsItemIds, 
			@Param("counts")String counts, @Param("cartIds")String cartIds,@Param("orderNo")String orderNo, 
			HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		int isInvoice = -1;
		String invoiceHead = "";
		int buyerId = Integer.parseInt(getCookieValueByName(request, "memberId"));
		BigDecimal priceForPay = new BigDecimal(0);
		
		//验证订单是否重复
		List<Map> order = orderDao.getOrderByNo(orderNo);
		if(order != null && order.size() > 0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_4); //该订单已存在，不要重复提交
			return "@" + jo.toString();
		}
		
		//验证库存
		if(goodsItemIds != null && goodsItemIds.length() > 0 && 
				counts != null && counts.length() > 0){
			String[] itemIds = goodsItemIds.split(",");
			String[] itemCnts = counts.split(",");
			if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
					itemCnts.length > 0 && itemIds.length == itemCnts.length){
				for(int i = 0; i < itemIds.length; i++){
					int goodsItemId = Integer.parseInt(itemIds[i]);
					int count =  Integer.parseInt(itemCnts[i]);
					
					List<Map> goodsItem = goodsDao.getGoodsItemById(goodsItemId);
					if(goodsItem != null && goodsItem.size() > 0){
						Map obj = goodsItem.get(0);
						int inventory = Integer.parseInt(obj.get("inventory").toString());  //验证库存
						if(inventory < count){
							jo.put("info", obj.get("name").toString() + "库存不足");
							jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
							jo.put("returnCode", Constants.returnObj.returnCode.code_1); //XXX库存不足
							return "@" + jo.toString();
						}
						
						String itemPrice = obj.get("discntPrice").toString();
						BigDecimal itemsTotal = new BigDecimal(itemPrice).multiply(new BigDecimal(count));
						priceForPay = priceForPay.add(itemsTotal);   //计算订单总价
					}
					else{
						jo.put("returnFlag", Constants.returnObj.returnFlag_flase); 
						jo.put("returnCode", Constants.returnObj.returnCode.code_2);//包含下架商品
						return "@" + jo.toString();
					}
				}
			}
			else{
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				jo.put("returnCode", Constants.returnObj.returnCode.code_3);//访问错误
				return "@" + jo.toString();
			}
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_3);//访问错误
			return "@" + jo.toString();
		}
		
		String phone = memberDAO.getPhoneById(buyerId);
		String receiverName = "";
		String receiverPhone = "";
		String receiverAddress = "";
		List<Map> address = memberDAO.getAddressById(addressId);
		if(address != null && address.size() > 0){
			receiverName = address.get(0).get("receiverName").toString();
			receiverPhone = address.get(0).get("receiverPhone").toString();
			String province = address.get(0).get("province").toString();
			String city = address.get(0).get("city").toString();
			String area = address.get(0).get("area").toString();
			String addr = address.get(0).get("address").toString();
			
			if( city.indexOf(province) > -1 ){
				receiverAddress = city + area + addr;
			}
			else{
				receiverAddress = province + city + area + addr;
			}
		}
		int status = SCOrderStatusEnum.prepay.getValue();
		
		priceForPay = priceForPay.add(new BigDecimal(LOGISTICS_FEE));//订单总价 + 运费
		String discntPrice = priceFormat(priceForPay.doubleValue()); //订单价
		int orderId = orderDao.createOrder(buyerId,sellerId,orderNo,discntPrice,receiverName,receiverPhone,receiverAddress,
				isInvoice,invoiceHead,memo,status,phone);
		
		//生成订单详情
		if(goodsItemIds != null && goodsItemIds.length() > 0 && 
				counts != null && counts.length() > 0){
			String[] itemIds = goodsItemIds.split(",");
			String[] itemCnts = counts.split(",");
			if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
					itemCnts.length > 0 && itemIds.length == itemCnts.length){
				for(int i = 0; i < itemIds.length; i++){
					int goodsItemId = Integer.parseInt(itemIds[i]);
					int count =  Integer.parseInt(itemCnts[i]);
					orderDao.createOrderItem(orderId,goodsItemId,count,phone);
				}
			}
		}
		
		//如果是从购物车生成订单，删除购物车
		if( cartIds != null && cartIds.length() > 0 ){
			String[] arrCartIds = cartIds.split(",");
			if( arrCartIds!= null && arrCartIds.length > 0){
				List<Integer> arrIds = new ArrayList<Integer>();
				for(int i = 0; i < arrCartIds.length; i++){
					arrIds.add(Integer.valueOf(arrCartIds[i]));
				}
				orderDao.delCartByIds(arrIds);
			}
		}
		
		//调用支付宝接口
		jo.put("orderId", orderId);
		return "@" + jo.toString();
	}
	
	@Post("settleZCAccounts")
	public String settleZCAccounts(@Param("sellerId")int sellerId, @Param("addressId")String addressId, @Param("memo")String memo, 
			@Param("goodsItemIds")String goodsItemIds,@Param("counts")String counts, @Param("cartIds")String cartIds,
			@Param("orderNo")String orderNo,@Param("gameId")int gameId,@Param("code")String code, HttpServletRequest request) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		int isInvoice = -1;
		String invoiceHead = "";
		int buyerId = Integer.parseInt(getCookieValueByName(request, "memberId"));
		BigDecimal priceForPay = new BigDecimal(0);
		
		//验证订单是否重复
		List<Map> order = orderDao.getOrderByNo(orderNo);
		if(order != null && order.size() > 0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_4); //该订单已存在，不要重复提交
			return "@" + jo.toString();
		}
		
		//活动信息
		List<Map> replis = applyDao.queryByGameIdAndCode(gameId, code);
		if(replis==null || replis.size()==0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_5); //不存在
			return "@" + jo.toString();
		}
		
		String level = replis.get(0).get("level")==null?"":replis.get(0).get("level").toString();
		if("".equals(level)){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_6); //无获奖信息
			return "@" + jo.toString();
		}
		
		//验证库存
		if(goodsItemIds != null && goodsItemIds.length() > 0 && 
				counts != null && counts.length() > 0){
			String[] itemIds = goodsItemIds.split(",");
			String[] itemCnts = counts.split(",");
			if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
					itemCnts.length > 0 && itemIds.length == itemCnts.length){
				for(int i = 0; i < itemIds.length; i++){
					int goodsItemId = Integer.parseInt(itemIds[i]);
					int count =  Integer.parseInt(itemCnts[i]);
					
					List<Map> goodsItem = goodsDao.getGoodsItemById(goodsItemId);
					if(goodsItem != null && goodsItem.size() > 0){
						Map obj = goodsItem.get(0);
						int inventory = Integer.parseInt(obj.get("inventory").toString());  //验证库存
						if(inventory < count){
							jo.put("info", obj.get("name").toString() + "库存不足");
							jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
							jo.put("returnCode", Constants.returnObj.returnCode.code_1); //XXX库存不足
							return "@" + jo.toString();
						}
						
						String itemPrice = obj.get("discntPrice").toString();
						BigDecimal itemsTotal = new BigDecimal(itemPrice).multiply(new BigDecimal(count));
						priceForPay = priceForPay.add(itemsTotal);   //计算订单总价
					}
					else{
						jo.put("returnFlag", Constants.returnObj.returnFlag_flase); 
						jo.put("returnCode", Constants.returnObj.returnCode.code_2);//包含下架商品
						return "@" + jo.toString();
					}
				}
			}
			else{
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				jo.put("returnCode", Constants.returnObj.returnCode.code_3);//访问错误
				return "@" + jo.toString();
			}
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_3);//访问错误
			return "@" + jo.toString();
		}
		
		String phone = memberDAO.getPhoneById(buyerId);
		String receiverName = "";
		String receiverPhone = "";
		String receiverAddress = "";
		List<Map> address = memberDAO.getAddressById(addressId);
		if(address != null && address.size() > 0){
			receiverName = address.get(0).get("receiverName").toString();
			receiverPhone = address.get(0).get("receiverPhone").toString();
			String province = address.get(0).get("province").toString();
			String city = address.get(0).get("city").toString();
			String area = address.get(0).get("area").toString();
			String addr = address.get(0).get("address").toString();
			
			if( city.indexOf(province) > -1 ){
				receiverAddress = city + area + addr;
			}
			else{
				receiverAddress = province + city + area + addr;
			}
		}
		int status = SCOrderStatusEnum.prepay.getValue();
		
		priceForPay = priceForPay.add(new BigDecimal(0));//订单总价 + 运费
		String discntPrice = priceFormat(priceForPay.doubleValue()); //订单价
		int orderId = scCrowdFundOrderDao.createOrder(buyerId,sellerId,orderNo,discntPrice,receiverName,receiverPhone,receiverAddress,
				isInvoice,invoiceHead,memo,status,phone,gameId,code,PROGRESS);
		
		//生成订单详情
		if(goodsItemIds != null && goodsItemIds.length() > 0 && 
				counts != null && counts.length() > 0){
			String[] itemIds = goodsItemIds.split(",");
			String[] itemCnts = counts.split(",");
			if(itemIds != null && itemIds.length > 0 && itemCnts != null && 
					itemCnts.length > 0 && itemIds.length == itemCnts.length){
				for(int i = 0; i < itemIds.length; i++){
					int goodsItemId = Integer.parseInt(itemIds[i]);
					int count =  Integer.parseInt(itemCnts[i]);
					scCrowdFundOrderDao.createOrderItem(orderId,goodsItemId,count,phone);
				}
			}
		}
		
		//如果是从购物车生成订单，删除购物车
		if( cartIds != null && cartIds.length() > 0 ){
			String[] arrCartIds = cartIds.split(",");
			if( arrCartIds!= null && arrCartIds.length > 0){
				List<Integer> arrIds = new ArrayList<Integer>();
				for(int i = 0; i < arrCartIds.length; i++){
					arrIds.add(Integer.valueOf(arrCartIds[i]));
				}
				scCrowdFundOrderDao.delCartByIds(arrIds);
			}
		}
		
		//调用支付宝接口
		jo.put("orderId", orderId);
		return "@" + jo.toString();
	}
	
	private String priceFormat(double price){
		DecimalFormat df = new DecimalFormat("#.##");
		return df.format(price);
	}
}
